clear all
set more off , permanently
capture log close
set scheme s1color
graph set window fontface "Garamond"
set type double


********************************************************************************
*** This file analyzes the effect of MPD on the VOI and computes daily p_min ***
********************************************************************************

	
****************		
*** Gasoline ***
****************
		
// Prepare market-level station ID dataset in Germany for merge (5 km markets)

	* Load data
		cd "$dta"
		use "station_markets_5.dta" , replace

	* Keep essential variables and reshape to have all station IDs for every overlapping market
		keep master_id_data using_id_data
		bysort master_id_data: gen comp_nr = _n
		summarize comp_nr, detail
		local max_comp = r(max)
		rename using_id_data id_seller_
		encode master_id_data , g(master_id_enc)
		drop master_id_data
		reshape wide id_seller_ , i(master_id_enc) j(comp_nr)
		g mkt_id = _n
		drop master_id_enc
		order mkt_id
		
		tempfile market_station_ids
		save `market_station_ids' , replace
		
		
//  Prepare German station-level price data for merge

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear
		drop if country == "fr"

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)

	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace e5_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)				
		
	* Gasoline analysis
*		drop if e5_17oclock_panel == .
		keep station_id date e5_17oclock_panel
		
	* Save tempfile
		tempfile prices_de_gasoline
		save `prices_de_gasoline' , replace
		

// Merge price data with market IDs for DE

	* Joinby for first seller
		use `market_station_ids' , clear
		rename id_seller_1 station_id
		joinby station_id using `prices_de_gasoline'
		drop if mkt_id == .
		rename e5_17oclock_panel gas_price_seller_1
		rename station_id id_seller_1
		tempfile mkt_station_price
		save `mkt_station_price' , replace		

	* Merge
		foreach x of num 2(1)`max_comp' {
			use `mkt_station_price' , clear
			rename id_seller_`x' station_id
			merge m:1 station_id date using `prices_de_gasoline'
			drop if _m == 2
			drop _merge
			rename e5_17oclock_panel gas_price_seller_`x'
			rename station_id id_seller_`x'
			save `mkt_station_price' , replace		
		}
		
	* Calculate the average price, minimum price, and VOI on every day in every market
		keep mkt_id date gas_price_seller*
		egen mkt_avg_p = rowmean(gas_price_seller*)
		egen mkt_min_p = rowmin(gas_price_seller*)
		gen voi = mkt_avg_p - mkt_min_p
		g local_monopolist = 0
		replace local_monopolist = 1 if gas_price_seller_2 == .
		drop gas_price_seller*
		
		g country = "de"
		order country mkt_id local_monopolist
		
	* Save data
		cd "$dta"
		save "mkt5km_price_gasoline_de.dta" , replace


		
		

///// France /////
			
// Prepare market-level station ID dataset in France for merge (5 km markets)

	* Load data
		cd "$dta"
		use "station_markets_5_france.dta" , replace

	* Keep essential variables and reshape to have all station IDs for every overlapping market
		keep master_id_data using_id_data
		bysort master_id_data: gen comp_nr = _n
		summarize comp_nr, detail
		local max_comp = r(max)
		rename using_id_data id_seller_
		encode master_id_data , g(master_id_enc)
		drop master_id_data
		reshape wide id_seller_ , i(master_id_enc) j(comp_nr)
		g mkt_id = _n
		drop master_id_enc
		order mkt_id
		
		tempfile market_station_ids
		save `market_station_ids' , replace
		
		
//  Prepare French station-level price data for merge

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear
		drop if country == "de"

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)

	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace e5_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)				
		
	* Gasoline analysis
*		drop if e5_17oclock_panel == .
		keep station_id date e5_17oclock_panel
		
	* Save tempfile
		tempfile prices_fr_gasoline
		save `prices_fr_gasoline' , replace
		

// Merge price data with market IDs for FR

	* Joinby for first seller
		use `market_station_ids' , clear
		rename id_seller_1 station_id
		joinby station_id using `prices_fr_gasoline'
		drop if mkt_id == .
		rename e5_17oclock_panel gas_price_seller_1
		rename station_id id_seller_1
		tempfile mkt_station_price
		save `mkt_station_price' , replace		

	* Merge
		foreach x of num 2(1)`max_comp' {
			use `mkt_station_price' , clear
			rename id_seller_`x' station_id
			merge m:1 station_id date using `prices_fr_gasoline'
			drop if _m == 2
			drop _merge
			rename e5_17oclock_panel gas_price_seller_`x'
			rename station_id id_seller_`x'
			save `mkt_station_price' , replace		
		}
		
	* Calculate the average price, minimum price, and VOI on every day in every market
		keep mkt_id date gas_price_seller*
		egen mkt_avg_p = rowmean(gas_price_seller*)
		egen mkt_min_p = rowmin(gas_price_seller*)
		gen voi = mkt_avg_p - mkt_min_p
		g local_monopolist = 0
		replace local_monopolist = 1 if gas_price_seller_2 == .
		drop gas_price_seller*
		
		g country = "fr"
		order country mkt_id local_monopolist
		
	* Save data
		cd "$dta"
		save "mkt5km_price_gasoline_fr.dta" , replace
		

		
**************		
*** Diesel ***
**************

///// Germany /////

// Prepare market-level station ID dataset in Germany for merge (5 km markets)

	* Load data
		cd "$dta"
		use "station_markets_5.dta" , replace

	* Keep essential variables and reshape to have all station IDs for every overlapping market
		keep master_id_data using_id_data
		bysort master_id_data: gen comp_nr = _n
		summarize comp_nr, detail
		local max_comp = r(max)
		rename using_id_data id_seller_
		encode master_id_data , g(master_id_enc)
		drop master_id_data
		reshape wide id_seller_ , i(master_id_enc) j(comp_nr)
		g mkt_id = _n
		drop master_id_enc
		order mkt_id
		
		tempfile market_station_ids
		save `market_station_ids' , replace
		
		
//  Prepare German station-level price data for merge

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear
		drop if country == "fr"

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)

	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace gazole_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)				
		
	* Gasoline analysis
*		drop if gazole_17oclock_panel == .
		keep station_id date gazole_17oclock_panel
		
	* Save tempfile
		tempfile prices_de_diesel
		save `prices_de_diesel' , replace
		

// Merge price data with market IDs for DE

	* Joinby for first seller
		use `market_station_ids' , clear
		rename id_seller_1 station_id
		joinby station_id using `prices_de_diesel'
		drop if mkt_id == .
		rename gazole_17oclock_panel dsl_price_seller_1
		rename station_id id_seller_1
		tempfile mkt_station_price
		save `mkt_station_price' , replace		

	* Merge
		foreach x of num 2(1)`max_comp' {
			use `mkt_station_price' , clear
			rename id_seller_`x' station_id
			merge m:1 station_id date using `prices_de_diesel'
			drop if _m == 2
			drop _merge
			rename gazole_17oclock_panel dsl_price_seller_`x'
			rename station_id id_seller_`x'
			save `mkt_station_price' , replace		
		}
		
	* Calculate the average price, minimum price, and VOI on every day in every market
		keep mkt_id date dsl_price_seller*
		egen mkt_avg_p = rowmean(dsl_price_seller*)
		egen mkt_min_p = rowmin(dsl_price_seller*)
		gen voi = mkt_avg_p - mkt_min_p
		g local_monopolist = 0
		replace local_monopolist = 1 if dsl_price_seller_2 == .
		drop dsl_price_seller*
		
		g country = "de"
		order country mkt_id local_monopolist
		
	* Save data
		cd "$dta"
		save "mkt5km_price_diesel_de.dta" , replace

		
		

///// France /////	
		
// Prepare market-level station ID dataset in France for merge (5 km markets)

	* Load data
		cd "$dta"
		use "station_markets_5_france.dta" , replace

	* Keep essential variables and reshape to have all station IDs for every overlapping market
		keep master_id_data using_id_data
		bysort master_id_data: gen comp_nr = _n
		summarize comp_nr, detail
		local max_comp = r(max)
		rename using_id_data id_seller_
		encode master_id_data , g(master_id_enc)
		drop master_id_data
		reshape wide id_seller_ , i(master_id_enc) j(comp_nr)
		g mkt_id = _n
		drop master_id_enc
		order mkt_id
		
		tempfile market_station_ids
		save `market_station_ids' , replace
		
		
//  Prepare French station-level price data for merge

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear
		drop if country == "de"

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)

	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace gazole_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)				
		
	* Gasoline analysis
*		drop if gazole_17oclock_panel == .
		keep station_id date gazole_17oclock_panel
		
	* Save tempfile
		tempfile prices_fr_diesel
		save `prices_fr_diesel' , replace
		

// Merge price data with market IDs for FR

	* Joinby for first seller
		use `market_station_ids' , clear
		rename id_seller_1 station_id
		joinby station_id using `prices_fr_diesel'
		drop if mkt_id == .
		rename gazole_17oclock_panel dsl_price_seller_1
		rename station_id id_seller_1
		tempfile mkt_station_price
		save `mkt_station_price' , replace		

	* Merge
		foreach x of num 2(1)`max_comp' {
			use `mkt_station_price' , clear
			rename id_seller_`x' station_id
			merge m:1 station_id date using `prices_fr_diesel'
			drop if _m == 2
			drop _merge
			rename gazole_17oclock_panel dsl_price_seller_`x'
			rename station_id id_seller_`x'
			save `mkt_station_price' , replace		
		}
		
	* Calculate the average price, minimum price, and VOI on every day in every market
		keep mkt_id date dsl_price_seller*
		egen mkt_avg_p = rowmean(dsl_price_seller*)
		egen mkt_min_p = rowmin(dsl_price_seller*)
		gen voi = mkt_avg_p - mkt_min_p
		g local_monopolist = 0
		replace local_monopolist = 1 if dsl_price_seller_2 == .
		drop dsl_price_seller*
		
		g country = "fr"
		order country mkt_id local_monopolist
		
	* Save data
		cd "$dta"
		save "mkt5km_price_diesel_fr.dta" , replace

		
		
		
		
		
		
****************		
*** Analysis ***	
****************		
		
	** Germany vs. France: Plot average VOI for gasoline and diesel over time	
		cd "$dta"
		use "mkt5km_price_gasoline_de.dta" , clear
		append using "mkt5km_price_gasoline_fr.dta"
		
	* Replace all VOI between 12 September 2013 and 01 October 2013 as 0 (and subsequently as missing again -- after collapse)
		replace voi = 0 if date > d(12sep2013) & date < d(01oct2013)
		replace local_monopolist = 0 if date >= d(12sep2013) & date < d(01oct2013)
		
	* Convert into Eurocent
		replace voi = voi * 100
				
		keep if local_monopolist == 0
		collapse (mean) voi , by(date country)
		replace voi = . if date >= d(12sep2013) & date < d(01oct2013)				
		reshape wide voi , i(date) j(country) string
		
	* Generate Figure 5a
		twoway (line voifr date ,  lpattern(solid) cmissing(n)) (line voide date ,  lpattern(dash) cmissing(n)), ///
			ytitle("Value of information (Eurocent)", size(medium)) ///
			legend(label(1 "France") label(2 "Germany") rows(1) rowgap(*.2) symx(*1) size(medsmall)) xline(`=d(12sep2013)', lcolor(gs0) lwidth(medium)) ///
			xtitle("") xlabel( `=d(01may2013)' "May 2013" `=d(01aug2013)' "August 2013" `=d(01nov2013)' "November 2013" `=d(01feb2014)' "February 2014" `=d(01may2014)' "May 2014" `=d(01aug2014)' "August 2014", labs(medsmall)) ///
			ylabel(0(1)6, grid labs(medsmall)) bgcolor(white) 

		graph export "$output/Figure5a_VOI_5km_gasoline_color.pdf", replace

	
	* PDF Format for JPE Micro
		twoway (line voifr date ,  lpattern(solid) cmissing(n) lcolor(black)) (line voide date ,  lpattern(dash) cmissing(n) lcolor(gs0)), ///
			ytitle("Value of information (Eurocent)") ///
			legend(label(1 "France") label(2 "Germany") rows(1) rowgap(*.2) symx(*1) ) xline(`=d(12sep2013)', lcolor(gs0) lwidth(medium)) ///
			xtitle("") xlabel( `=d(01may2013)' "May 2013" `=d(01aug2013)' "August 2013" `=d(01nov2013)' "November 2013" `=d(01feb2014)' "February 2014" `=d(01may2014)' "May 2014" `=d(01aug2014)' "August 2014") ///
			ylabel(0(1)6, grid ) bgcolor(white) ysize(6) xsize(9) scale(1)

		graph export "$output/Figure5a_VOI_5km_gasoline_bw.pdf", replace
		
		
		cd "$dta"
		use "mkt5km_price_diesel_de.dta" , clear
		append using "mkt5km_price_diesel_fr.dta"
		
	* Replace all VOI between 12 September 2013 and 01 October 2013 as 0 (and subsequently as missing again -- after collapse)
		replace voi = 0 if date > d(12sep2013) & date < d(01oct2013)
		replace local_monopolist = 0 if date >= d(12sep2013) & date < d(01oct2013)
		
	* Convert into Eurocent
		replace voi = voi * 100
				
		keep if local_monopolist == 0
		collapse (mean) voi , by(date country)
		replace voi = . if date >= d(12sep2013) & date < d(01oct2013)				
		reshape wide voi , i(date) j(country) string
		
		
	* Generate Figure 5b
		twoway (line voifr date ,  lpattern(solid) cmissing(n)) (line voide date ,  lpattern(dash) cmissing(n)), ///
			ytitle("Value of information (Eurocent)", size(medium)) ///
			legend(label(1 "France") label(2 "Germany") rows(1) rowgap(*.2) symx(*1) size(medsmall)) xline(`=d(12sep2013)', lcolor(gs0) lwidth(medium)) ///
			xtitle("") xlabel( `=d(01may2013)' "May 2013" `=d(01aug2013)' "August 2013" `=d(01nov2013)' "November 2013" `=d(01feb2014)' "February 2014" `=d(01may2014)' "May 2014" `=d(01aug2014)' "August 2014", labs(medsmall)) ///
			ylabel(0(1)6, grid labs(medsmall)) bgcolor(white) 

		graph export "$output/Figure5b_VOI_5km_diesel_color.pdf", replace			

		
	* PDF Format for JPE Micro		
		twoway (line voifr date ,  lpattern(solid) cmissing(n) lcolor(black)) (line voide date ,  lpattern(dash) cmissing(n) lcolor(gs0)), ///
			ytitle("Value of information (Eurocent)") ///
			legend(label(1 "France") label(2 "Germany") rows(1) rowgap(*.2) symx(*1) ) xline(`=d(12sep2013)', lcolor(gs0) lwidth(medium)) ///
			xtitle("") xlabel( `=d(01may2013)' "May 2013" `=d(01aug2013)' "August 2013" `=d(01nov2013)' "November 2013" `=d(01feb2014)' "February 2014" `=d(01may2014)' "May 2014" `=d(01aug2014)' "August 2014") ///
			ylabel(0(1)6, grid ) bgcolor(white) ysize(6) xsize(9) scale(1)

		graph export "$output/Figure5b_VOI_5km_diesel_bw.pdf", replace			
		
		

	** Germany vs. France: Compute MPD effect on average to avg price and minimum price
		
		eststo clear
		
		// 5 km market gasoline //
				cd "$dta"
				use "mkt5km_price_gasoline_de.dta" , clear
				append using "mkt5km_price_gasoline_fr.dta"

			* Keep only for restricted analysis period
				keep if date > d(12apr2013) & date < d(31mar2014)
			
			* Generate treatment variable
				gen treatment_group = 0
				replace treatment_group = 1 if country == "de"
				gen treatment_period = 0
				replace treatment_period = 1 if date >= date("20130912","YMD")

				drop if mkt_avg_p == .

			* Define treatment variable
				gen treated = 0
				replace treated = 1 if treatment_group == 1 & treatment_period == 1
				
			* Replace all prices between 12 September 2013 and 01 October 2013 as missing
				replace mkt_avg_p = . if date > d(12sep2013) & date < d(01oct2013)
				replace mkt_min_p = . if date > d(12sep2013) & date < d(01oct2013)

			* Keep only necessary variables for station level pass-through estimation
				keep mkt_id date mkt_avg_p mkt_min_p country treated
					
			* Set panel structure
				replace mkt_id = mkt_id + 100000 if country == "fr"	
				g id_help = "zzzz-FR_" if country == "fr"
				egen mkt_id_aux = concat(id_help mkt_id)
				egen id=group(mkt_id_aux)														// numeric values for German vs French station ids are in different ranges
				drop mkt_id_aux id_help
				xtset id date

			* Gen log price
				gen log_avg_p = log(mkt_avg_p)
				gen log_min_p = log(mkt_min_p)
				drop mkt_avg_p mkt_min_p
				
			* String variable for regression
				tostring mkt_id , replace

			* Run MPD effect regression for average and minimum price
				eststo mpd_5km_pavg_gas: reghdfe log_avg_p treated , ab(mkt_id date) cl(mkt_id)
				estadd ysumm
				eststo mpd_5km_pmin_gas: reghdfe log_min_p treated , ab(mkt_id date) cl(mkt_id)						
				estadd ysumm
		
		
		// 5 km market diesel //
				cd "$dta"
				use "mkt5km_price_diesel_de.dta" , clear
				append using "mkt5km_price_diesel_fr.dta"

			* Keep only for restricted analysis period
				keep if date > d(12apr2013) & date < d(31mar2014)
			
			* Generate treatment variable
				gen treatment_group = 0
				replace treatment_group = 1 if country == "de"
				gen treatment_period = 0
				replace treatment_period = 1 if date > date("20130912","YMD")

				drop if mkt_avg_p == .

			* Define treatment variable
				gen treated = 0
				replace treated = 1 if treatment_group == 1 & treatment_period == 1
				
			* Replace all prices between 12 September 2013 and 01 October 2013 as missing
				replace mkt_avg_p = . if date > d(12sep2013) & date < d(01oct2013)
				replace mkt_min_p = . if date > d(12sep2013) & date < d(01oct2013)

			* Keep only necessary variables for station level pass-through estimation
				keep mkt_id date mkt_avg_p mkt_min_p country treated local_monopolist
					
			* Set panel structure
				replace mkt_id = mkt_id + 100000 if country == "fr"	
				g id_help = "zzzz-FR_" if country == "fr"
				egen mkt_id_aux = concat(id_help mkt_id)
				egen id=group(mkt_id_aux)														// numeric values for German vs French station ids are in different ranges
				drop mkt_id_aux id_help
				xtset id date

			* Gen log price
				gen log_avg_p = log(mkt_avg_p)
				gen log_min_p = log(mkt_min_p)		
				drop mkt_avg_p mkt_min_p
				
			* String variable for regression
				tostring mkt_id , replace

			* Run MPD effect regression for average and minimum price
				eststo mpd_5km_pavg_diesel: reghdfe log_avg_p treated , ab(mkt_id date) cl(mkt_id)
				estadd ysumm
				eststo mpd_5km_pmin_diesel: reghdfe log_min_p treated , ab(mkt_id date) cl(mkt_id)						
				estadd ysumm

		
			* Gen Table 3: Effect of radio reports on the logarithm of gross prices
				esttab mpd_5km_pavg_gas mpd_5km_pmin_gas mpd_5km_pavg_diesel mpd_5km_pmin_diesel using "$output\Table 3_mpd_market.tex" , b(4) se(4) ar2 replace booktabs keep(treated) star(* 0.10 ** 0.05 *** 0.01) label ///
				title(Effect of MPD on the logarithm of market-level prices) ///
				nonumbers mtitles("(1)E5 \textit{Average}" "(2)E5 \textit{Minimum}" "(3)Diesel \textit{Average}" "(4)Diesel \textit{Minimum}") ///
				addnote("SEs are clustered at the fuel station level.")		
		
